package org.janon.httpproxy;

import java.net.ServerSocket;
import java.net.Socket;

import org.janon.httpproxy.client.Client;
import org.janon.httpproxy.server.Server;

public class testConnectMethod {
	public static void main(String[] args) throws Exception {
		Server server = new Server(801);
		byte[] buf = new byte[96000];
		int read = 0;
		server.start();
		
		Client client = new Client("127.0.0.1",801);
		client.start();
		
		Socket socketSend = new Socket("127.0.0.1",9000);
		ServerSocket serverSocket = new ServerSocket(802);
		
		socketSend.getOutputStream().write(("CONNECT localhost:802 HTTP/1.1"+HttpUtil.CRLF).getBytes());
		socketSend.getOutputStream().write((HttpUtil.CRLF).getBytes());
		
		Socket socketGet = serverSocket.accept();
		
		read = socketSend.getInputStream().read(buf);
		System.out.println("\nclient read:");
		System.out.write(buf, 0, read);
		
		for(int i=0;i<2;i++){
			// client send
			socketSend.getOutputStream().write(("test "+i).getBytes());
			System.out.println("\nclient send:\n"+("test "+i));
			// server get
			read = socketGet.getInputStream().read(buf);
			System.out.println("\nserver read:");
			System.out.write(buf, 0, read);
			// server send
			socketGet.getOutputStream().write(("test "+i+" ok").getBytes());
			System.out.println("\nserver send:\n"+("test "+i+" ok"));
			// client get
			read = socketSend.getInputStream().read(buf);
			System.out.println("\nclient read:");
			System.out.write(buf, 0, read);
			if(i%2==0)
				Thread.sleep(3000);
			else
				Thread.sleep(20000);
		}
		socketSend.close();
		serverSocket.close();
		
		Thread[] list = new Thread[HttpUtil.getThreadGroup().activeCount()];
		HttpUtil.getThreadGroup().enumerate(list);
		for(int i=0,len=list.length;i<len;i++){
			list[i].stop();
		}
		client.stop();
		server.stop();
		socketGet.close();
		
	}
}
